Java megzine: Growing onopan by Michael Clarck & Manish Sahu

Java megzine: Growing onopan by Michael Clarck & Manish Sahu

Author:Michael Clarck & Manish Sahu [Clarck, Michael]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-13T16:00:00+00:00


Download all listings in this issue as text The listener object can be reused if necessary, but if it’s used only once, the code in Listing 6 is considered good style.

In this example, the slightly odd syntax involving extra curly brackets is needed because action Performed returns void. We’ll see more about that later.

Let’s move on to take a look at the role that lambda expressions will play in writing modern code for handling collections, in particular the transition between two programming styles known as external and internal iteration.

External Versus Internal

Iteration

Up until now, the standard way of dealing with a Java collection is via external iteration. It’s called external iteration because there is control flow, external to the collection, which is used to iterate over the elements contained in the collection. This is the tradi

tional way of handling collections,

with which most Java program

mers are very familiar—even if

they don’t know or use the term.

For example, language con

structs such as the enhanced for

loop create an external iterator

and use that object to traverse the

collection shown in Listing 7.

With this approach, a collection

class represents a “monolithic”

view of all the elements in the col

lection, and the collection object

can provide random access to any

element of the collection that the

programmer might require.

In this view of the world, we

iterate by calling a method—

iterator()—on the collection

instance. This returns an objectblogof type Iterator, which is a more

restricted view on the same

collection. It does not expose

an interface allowing random

access; instead, it is designed35 for purely sequential handling of the contents of the collection. This sequential nature

also gives rise to the infamous ConcurrentModificationException when you try to use the collection with parallel access.

The alternative is to require the collection object to manage the iterator (and loop) internally. This approach is called internal iteration, and it is the preferred choice when working with lambda expressions.

In addition to new syntax for lambda expressions, Project Lambda includes a major upgrade to the collections libraries. The aim of the collections upgrade is to make it much easier to write code that uses internal iteration to support a range of well-known functional programming idioms.

Functional Idioms with

Lambdas

At one time or another, most developers have found themselves needing to perform one or more of the following operations on a collection:

■■ Create a new collection, which

filters out any elements that don’t satisfy some condition ■ ■ Apply a transformation to each element of a collection, and work with the transformed collection

■ ■ Build up an overall value for a property of the whole collection, for example, adding up or averaging the values

These tasks (which are called,

respectively, the filter, map, and reduce idioms) have something important in common: They all require code to be run on each element of the collection in turn.

Whether the code is testing an element to see whether a condition is met (filter), transforming the element into a new element for a new collection (map), or computing a value to be fed into an overall calculation (reduce), the key theme is “a bit of code that needs to be run on each element.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.